//
//  WmcVideo.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/7/12.
//

import AVFoundation
import UIKit
import YPBedrock

public extension WmcVideo {
    enum VideoType {
        /// 无
        case none
        /// 通过数据加载
        case avAsset(_ avAsset: AVAsset)
        /// 通过Url加载
        case url(_ url: YPUnifiedUrl)
        /// 通过文件路径加载
        case filePath(_ filePath: String)

        var description: String {
            switch self {
            case .none: return "⚠️None！"
            case let .avAsset(avAsset): return "avAsset\(avAsset)"
            case let .url(value): return "🔗Url: \(value)"
            case let .filePath(value): return "🗃FilePath: \(value.description)"
            }
        }
    }
}

// MARK: - [WmcVideo]

open class WmcVideo: WmcMedia {
    // Lifecycle

    override public init() {
        super.init()
    }

    /// 通过数据加载
    public convenience init(avAsset: AVAsset) {
        self.init()
        videoType = .avAsset(avAsset)
    }

    /// 通过Url加载
    public convenience init(url: YPUnifiedUrl) {
        self.init()
        videoType = .url(url)
    }

    /// 通过文件路径加载
    public convenience init(filePath: String) {
        self.init()
        videoType = .filePath(filePath)
    }

    // Open

    /// 图片类型枚举
    open var videoType: VideoType = .none

    override open var description: String {
        let supDes = super.description
        return "\(supDes): \(videoType.description)"
    }

    open var url: URL? {
        switch videoType {
        case .none: return nil
        case .avAsset: return nil
        case let .url(url):
            let _url = URL.YP.Create(url)
            return _url
        case let .filePath(path):
            return path.yp.fileUrl
        }
    }

    open func loadAVAsset(successBlock: @escaping YPBlock.OneParam<AVAsset>,
                          failureBlock: YPBlock.OneParam<YPWmcError>? = nil)
    {
        switch videoType {
        case .none:
            let _log = "视频类型为none，无法加载"
            log(_log)
            failureBlock?(.init(errorType: .actionError, info: _log))

        case let .avAsset(avAsset):
            successBlock(avAsset)

        case let .url(url):
            if let url = URL.YP.CreateOptional(url) {
                let asset: AVAsset = .init(url: url)
                successBlock(asset)
            } else {
                let _log = "视频地址错误！请视频地址是否正确！"
                log(_log)
                failureBlock?(.init(errorType: .parsingError, info: _log))
            }

        case let .filePath(path):
            var filePath = path
            if !filePath.hasPrefix(YPSystem.Documents.Path) {
                filePath = YPSystem.Documents.Path
                    .yp.appendPathComponent(path)
            }
            if let fileUrl = filePath.yp.fileUrl_optional {
                let asset: AVAsset = .init(url: fileUrl)
                successBlock(asset)
            } else {
                let _log = "视频文件读取错误！请检查路径是否正确！\(filePath)"
                log(_log)
                failureBlock?(.init(errorType: .parsingError, info: _log))
            }
        }
    }

    // Internal

    open override func mediaType() -> WmcMedia.MediaType {
        return .video
    }

    // Fileprivate

    /// 日志系统
    fileprivate func log(_ log: String) {
        WmcLogManager.DebugLog(type: .wmcVideo, log: log)
    }
}
